﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class BuyerWeeklyPrice
    {
        public static DataSet listWithAvgSellerPrice(int bpwid, string where, int pageIndex, int pageSize)
        {
            SqlParameter[] param = new SqlParameter[4];

            param[0] = new SqlParameter("@bpwid", SqlDbType.Int);
            param[0].Value = bpwid;

            param[1] = new SqlParameter("@where", SqlDbType.NVarChar, 4000);
            param[1].Value = where;

            param[2] = new SqlParameter("@pageIndex", SqlDbType.Int, 4);
            param[2].Value = pageIndex;

            param[3] = new SqlParameter("@pageSize", SqlDbType.Int, 4);
            param[3].Value = pageSize;

            return DbHelper.ExecuteReDataSet("p_Product_listWithAvgSellerPrice", param);
        }

        /// <summary>
        /// 添加/编辑(1表示添加,2表示编辑)
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="bpwdid"></param>
        /// <param name="action">动作标识</param>
        /// <returns></returns>
        public static bool bulkInsert(DataTable dt, int bpwid, int action)
        {
            bool bulkCopyAffected = false;

            string M_ConnectionString = ConfigurationManager.ConnectionStrings["peisongDb"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(M_ConnectionString))
            {
                conn.Open();

                using (SqlCommand com = new SqlCommand("p_purchaseWeek_update", conn))
                {
                    SqlParameter param = new SqlParameter("@bwpid", SqlDbType.Int);
                    param.Value = bpwid;

                    com.CommandType = CommandType.StoredProcedure;
                    com.Parameters.Add(param);

                    int AffectedRows = com.ExecuteNonQuery();

                    if (AffectedRows >= 0)
                    {
                        // 编辑前删除当前数据
                        if (action == 2)
                        {
                            com.CommandText = "p_BuyerWeeklyPrice_delete";
                            AffectedRows = com.ExecuteNonQuery();
                        }

                        if (AffectedRows >= 0)
                        {
                            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
                            {
                                bulkCopy.DestinationTableName = "[dbo].[buyerWeeklyPrice]";

                                bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping("bpwid", "bpwid"));
                                bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping("productId", "productId"));
                                bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping("avgSellerPrice", "avgSellerPrice"));
                                bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping("priceRate", "priceRate"));
                                bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping("enabledDeduct", "enabledDeduct"));
                                bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping("initPriceRate", "initPriceRate"));

                                bulkCopy.WriteToServer(dt);
                                bulkCopyAffected = true;
                            }
                        }
                    }
                }
            }

            return bulkCopyAffected;
        }

        public static DataTable listWithCompanyWeek(int bpwdid)
        {
            return DbHelper.ExecuteDataTable("p_BuyerWeeklyPrice_listWithWeekCompany", bpwdid);
        }

        public static DataTable buyerWeeklyProductList(int companyId, int week)
        {
            return DbHelper.ExecuteDataTable("p_buyerWeeklyProduct_list", companyId, week);
        }
    }
}
